export default {
    namespaced: true,
    state: {
        type: 'all',
        list: JSON.parse(localStorage.getItem('list')) || [],
    },

    mutations: {
        //1.修改复选框的状态
        changeDone(state, id) {
            //find返回的数组中的满足条件的元素，这里是个对象
            const obj = state.list.find(item => item.id === id)
            obj.isDone = !obj.isDone

        },
        //2.删除任务
        del(state, id) {
            //filter筛选符合条件的元素组成的新数组 
            state.list = state.list.filter(item => item.id !== id)
        },
        //3.新增数据
        addList(state, content) {
            state.list.push({
                id: +new Date(),
                name: content,
                isDone: false,
            })
        },
        //4.实现全部选中 
        all(state, val) {
            return state.list.forEach((item) => (item.isDone = val));
        },
        //7.清除所有完成的任务
        clear(state) {
            state.list = state.list.filter(item => !item.isDone)
        },
        //8.切换高亮效果
        changType(state, type) {
            console.log(type);
            state.type = type
        }

    },
    actions: {

    },

    getters: {
        //5.底部显示未完成的数量
        length(state) {
            return state.list.filter(item => !item.isDone).length
        },
        //6.清除所有的按钮的显示 
        isShow(state) {
            return state.list.some(item => item.isDone)
        },
        //9.显示对应按钮的内容
        showList(state) {
            if (state.type === 'active') {
                return state.list.filter(item => !item.isDone)
            } else if (state.type === 'complete') {
                return state.list.filter(item => item.isDone)
            } else {
                return state.list
            }
        }

    },


}